package ${daoPackage};

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import ${impBeanPackage};

@Repository("${daoAnoName}")
public class ${daoName} {

	@Autowired
	private DataSource dataSource;
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	
	/**
	 * 查询
	 * @param ${beanName}
	 * @return
	 */
	public ${beanName} get${beanName}ById(final ${beanName} ${beanName?uncap_first}){
		String sql="select * from ${tableName} where ${tablePK.columnName}=?";
	    jdbcTemplate.query(sql, new Object[]{${beanName?uncap_first}.get${tablePK.fieldName?cap_first}()},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
			                                 <#list bean as b> 
			                                 <#if b.fieldType=="String">
			                                 ${beanName?uncap_first}.set${b.fieldName?cap_first}(rs.getString("${b.columnName}"));
			                                 <#elseif b.fieldType=="Date">
			                                 ${beanName?uncap_first}.set${b.fieldName?cap_first}(new Date(rs.getDate("${b.columnName}").getTime()));
			                                 <#elseif b.fieldType=="BigDecimal">
			                                 ${beanName?uncap_first}.set${b.fieldName?cap_first}(rs.getBigDecimal("${b.columnName}"));
			                                 </#if>
			                                 </#list>
				                             //task.setQuartz_abs(rs.getString("quartz_abs"));
				                        }          
		});  
		
		return ${beanName?uncap_first};
	}
	
	/**
	 * 添加
	 * @param ${beanName}
	 * @return
	 */
	public int add(${beanName} ${beanName?uncap_first}){
		String sql="insert into ${tableName}(<#list bean as b><#if b_has_next>${b.columnName},<#else>${b.columnName}</#if> </#list>) values(<#list bean as b><#if b_has_next && !b.isPk>?,<#elseif !b_has_next && !b.isPk>?<#elseif b_has_next && b.isPk>${tablePKSeq}.nextval,<#elseif !b_has_next && b.isPk>${tablePKSeq}.nextval</#if> </#list>)";
		return jdbcTemplate.update(sql, new Object[]{<#list bean as b><#if b_has_next && !b.isPk>${beanName?uncap_first}.get${b.fieldName?cap_first}(),<#elseif !b_has_next && !b.isPk>${beanName?uncap_first}.get${b.fieldName?cap_first}()</#if></#list>});
	}
	
	/**
	 * 更新
	 * @param ${beanName}
	 * @return
	 */
	public int update${beanName}(${beanName} ${beanName?uncap_first}){
		String sql="update ${tableName} set <#list bean as b><#if b_has_next && !b.isPk>${b.columnName}=?,<#elseif !b.isPk>${b.columnName}=?</#if> </#list> where ${tablePK.columnName}=? ";
		return jdbcTemplate.update(sql, new Object[]{<#list bean as b><#if !b.isPk>${beanName?uncap_first}.get${b.fieldName?cap_first}(),</#if></#list><#list bean as b><#if b.isPk>${beanName?uncap_first}.get${b.fieldName?cap_first}()</#if></#list>});
	}
	
	/**
	 * 删除 ${beanName}
	 * @param task
	 * @return
	 */
	public int del${beanName}(${beanName} ${beanName?uncap_first}){
		String sql="delete ${tableName}  where ${tablePK.columnName}=?";
		return jdbcTemplate.update(sql, new Object[]{${beanName?uncap_first}.get${tablePK.fieldName?cap_first}()});
	}
	
	/**
	 * 获取所有 ${beanName}
	 * @return
	 */
	public List<${beanName}>getAll${beanName}(){
		String sql="select * from ${tableName}";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(${beanName}.class));
	}
	
	
	/**
	 * 获取所有分页任务
	 * @return
	 */
	public List<${beanName}>getPageAll${beanName}(String sql){
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper(${beanName}.class));
	}
	
	
	/**
	 * 分页获取页数 
	 */
	public String getCountPage(String sql){
		String result=jdbcTemplate.queryForObject(sql,String.class);
		return result;
	}
	
}
